package study.jsm.com.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import study.jsm.com.myapplication.Utils.DBHelper;
import study.jsm.com.myapplication.Utils.StingUtils;
import study.jsm.com.myapplication.Utils.ToastUtils;

public class LoginActivity extends Activity {

    TextView tv_index;
    EditText et_account;
    EditText et_psd;
    CheckBox cb_user;

    SharedPreferences sp;

    String username;
    String password;

    DBHelper dbHelper;

//    String username_register;
//    String password_register;

//    @Override
//    protected void onResume(){
//        super.onResume();
//        username_register = sp.getString(StingUtils.USERNAME_REGISTER,"");
//       password_register = sp.getString(StingUtils.PASSWORD_REGISTER,"");
//    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        dbHelper = new DBHelper(this);
        super.onCreate(savedInstanceState);
        sp = LoginActivity.this.getSharedPreferences("user_info",Context.MODE_PRIVATE);


       username = sp.getString("username","");
       password = sp.getString("password","");

        if (checkUser(username,password)){
            goMainPage();
            finish();
        }

        setContentView(R.layout.activity_login);
        cb_user = findViewById(R.id.cb_user);
        et_account = findViewById(R.id.et_account);
        et_psd = findViewById(R.id.et_psd);
        tv_index = findViewById(R.id.tv_index);
        et_account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // 输入文本之前
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入文本过程中
            }

            @Override
            public void afterTextChanged(Editable s) {
                // 输入文本之后
                String str = s.toString();
                System.out.println("LoginActivity.afterTextChanged  || " + s.toString());
                tv_index.setText(str.length()+"/11");
            }
        });

        findViewById(R.id.tv_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isCheck = cb_user.isChecked();

                System.out.println("LoginActivity.onClick="+isCheck);

                String account = et_account.getText().toString();
                String psd = et_psd.getText().toString();

//                System.out.println("LoginActivity.onCreate username_register:"+username_register+"||password_register:"+password_register+"");
//                System.out.println("LoginActivity.onCreate account:"+account+"||psd:"+psd+"");

                if (checkUser(account,psd)){
                    ToastUtils.showToast(getApplicationContext(),"登录成功");

                    if (isCheck) {
                       SharedPreferences.Editor editor = sp.edit();
                       editor.putString("username",account);
                       editor.putString("password",psd);
                       editor.commit();
                    }
                    goMainPage();
                } else{
                    ToastUtils.showToast(getApplicationContext(),"登录成功");
                }
            }
        }) ;

        findViewById(R.id.tv_register).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
//                startActivityForResult(intent,1);
            }
        });

    }

    private boolean checkUser(String username,String password){

        if (!TextUtils.isEmpty(username)
                &&!TextUtils.isEmpty(password) ){
            String dbPsd = dbHelper.findPasswordByName(username);
            if (!TextUtils.isEmpty(dbPsd) && dbPsd.equals(password)){
                return true;
            }
        }
        return false;
    }

    private void goMainPage() {
        Intent intent = new Intent(LoginActivity.this,MainActivity.class);
        startActivity(intent);
        finish();
    }

   @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String res = data.getStringExtra("result");
        System.out.println("LoginActivity.onActivityResult res ="+res);
   }

    public void onClickClear(View view) {
        et_account.setText("");
        et_psd.setText("");
    }
}
